//app.js
const utils = require("utils/util.js")
const urls = require("utils/url.js").urls
const md5 = require("utils/md5.js").hex_md5
App({

	globalData: {
		userInfo: null,
		detail: {},
		datas: {
			netgates: [],
			
		}
	},

	// public functions
	...utils, md5,

	// ajax urls settings
	urls,
	
	
	
	// 获取所有网关
	getnetgates: function(callback) {
		let that = this
		let G = this.globalData
		this.get(this.urls.queryusergates, {token: G.detail.token}, res => {
			console.log("queryusergates => ", res)
			G.datas.netgates = res.data.netgates // 往公共变量中存网关数据
			callback()
		})
	},

	// 获取当前网关数据，存入公共变量，同时更新当前页面
	getcuridxdatas: function(idx, callback) {
		let that = this
		let G = this.globalData
		let id = G.datas.netgates[idx].id
		if(G.datas.netgates[idx]['sensor_data'] == undefined){
			// console.log('i showed load data')
			this.get(this.urls.queryplantsummary, {token: G.detail.token, id: id}, res => {
				console.log('queryplantsummary => ', res)
				G.datas.netgates[idx].plantsummary = res.data
				that.get(that.urls.querylastdevicedata, {token: G.detail.token, id: id}, res => {
					console.log('querylastdevicedata => ', res)
					let sort = ['tempture', 'humidity', 'earthTemp', 'earthHum', 'solar']
					let high = ['low', 'mid', 'high']
					let arr = []
					for(let i in sort){
						let x = res.data.data[0].sensor_data[sort[i]]
						x.high = high[x.level]
						arr.push(x)
					}
					G.datas.netgates[idx].sensor_data = arr
					that.get(that.urls.queryuserswitchs, {token: G.detail.token, id: id}, res => {
						console.log("queryuserswitchs => ", res)
						if(res.data['switch'] != undefined){
							G.datas.netgates[idx].switchs = res.data.switch
						}else{
							G.datas.netgates[idx].switchs = []
						}
						callback()
					})	
				})
			})
			
			
		}else{
			callback()
		}
		
	},

	
})